package com.zlll.winner.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.activity.ActivityApply;
import com.zlll.winner.business.model.activity.ActivityApplyExternal;
import com.zlll.winner.business.model.activity.ActivityManagement;
import com.zlll.winner.business.service.activity.IActivityApplyExternalService;
import com.zlll.winner.business.service.activity.IActivityApplyService;
import com.zlll.winner.business.service.activity.IActivityManagementService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api/activity")
@RestController
public class ActivityController {

    @Reference(version = "${dubbo.consumer.version}")
    private IActivityManagementService managementService;
    @Reference(version = "${dubbo.consumer.version}")
    private IActivityApplyExternalService externalService;
    @Reference(version = "${dubbo.consumer.version}")
    private IActivityApplyService applyService;

    /**
     * 首页活动列表
     * @param data
     * @return
     */
    @PostMapping("homePage")
    public Object homePage(@RequestBody JSONObject data){
        Integer pageSize = data.getInteger("pageSize") == null ? 5:data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1:data.getInteger("pageNo");
        String cityName = data.getString("cityName");
        return managementService.getHomePage(cityName,pageSize,pageNo).getRecords();
    }

    /**
     * 活动列表
     * @param data
     * @return
     */
    @PostMapping("activityList")
    public Object activityList(@RequestBody JSONObject data){
        Integer pageSize = data.getInteger("pageSize") == null ? 5:data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1:data.getInteger("pageNo");
        String cityName = data.getString("cityName");
        return managementService.getHomePage(cityName,pageSize,pageNo);
    }

    /**
     * 活动详情
     * @param data
     * @return
     */
    @PostMapping("activityInfo")
    public Object activityInfo(@RequestBody JSONObject data){
        Long id = data.getLong("id");
        Assert.notNull(id, "请传入id");

        ActivityManagement management = managementService.getById(id);
        List<ActivityApplyExternal> list = externalService.getExternalByActivityId(id);
        Map result = new HashMap();
        result.put("activity",management);
        result.put("external",list);
        return result;
    }

    /**
     * 活动报名
     * @param apply
     * @return
     */
    @PostMapping("apply")
    public Object apply(@RequestBody ActivityApply apply)throws Exception{
        Assert.notNull(apply.getActivityId(), "请传入活动Id");
        Assert.notNull(apply.getName(), "请传入申请人名称");
        Assert.notNull(apply.getTelphone(), "请传入申请人手机");
        Assert.notNull(apply.getNum(), "请传入人数");


//        Boolean isApply = managementService.isApply(apply.getActivityId());
//        if(!isApply){
//            throw new Exception("报名人数已满！");
//        }

        return applyService.saveApply(apply);
    }

    /**
     * 报名列表
     * @param data
     * @return
     */
    @PostMapping("userApplyList")
    public Object userApplyList(@RequestBody JSONObject data){

        Long userId = data.getLong("userId");
        Integer pageSize = data.getInteger("pageSize") == null ? 5:data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1:data.getInteger("pageNo");

        return applyService.getApplyByUserId(userId,pageNo,pageSize);
    }

    @GetMapping("/todayUpdate")
    public Object todayUpdate(){
         managementService.todayUpdate();
         return true;
    }
}
